﻿using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Threading;
using CSUploadOSUI.ViewModel;

namespace CSUploadOSUI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        private CsUploadTool _csuploadtool;
        private uint _outPutLinesCount;
        private string _headerCache;

        public event OutputPipeLine OutputLineEvent
        {
            add
            {
                if (_csuploadtool != null)
                    _csuploadtool.Newline += value;
            }
            remove
            {
                if (_csuploadtool != null)
                    _csuploadtool.Newline -= value;
            }
        }

        public MainWindow()
        {
            InitializeComponent();

            _csuploadtool= new CsUploadTool();
            OutputLineEvent += MainWindow_OutputLineEvent;

            txtResult.Document.LineHeight = 1;

            DataContext = new Model();
            txtSubscriptionId.DataContext = "SubscriptionId";
        }

        void MainWindow_OutputLineEvent(string results)
        {
            Debug.Print(results);
            txtResult
                .Dispatcher
                .BeginInvoke(DispatcherPriority.Send,
                             (Action) (() =>
                                           {
                                               if (_outPutLinesCount < 3)
                                               {
                                                   _headerCache += results + "\n";
                                               }
                                               else if (_outPutLinesCount == 3)
                                               {
                                                   var tr =
                                                       new TextRange(txtResult.Document.ContentEnd,
                                                                     txtResult.Document.ContentEnd)
                                                           {Text = _headerCache};

                                                   tr.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);
                                                   tr.ApplyPropertyValue(TextElement.FontWeightProperty,
                                                                         FontWeights.Bold);
                                               }
                                               else
                                               {
                                                   var lb = new LineBreak();
                                                   var p = new Paragraph();

                                                   p.Inlines.Add(lb);
                                                   txtResult.Document.Blocks.Add(p);
                                                   txtResult.AppendText(results);
                                               }

                                               _outPutLinesCount++;

                                               txtResult.ScrollToEnd();
                                           }));
        }

        private void button10_Click(object sender, RoutedEventArgs e)
        {
            _csuploadtool.Test();
        }

        private void btnGetConn_Click(object sender, RoutedEventArgs e)
        {
            _csuploadtool.GetConnection();
        }

        private void button8_Click(object sender, RoutedEventArgs e)
        {
            _csuploadtool.SetConnection(txtSubscriptionId.Text, txtCertificateThumb.Text);
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {

            var dlg = new Microsoft.Win32.OpenFileDialog
                          {
                              //FileName = "*",
                              DefaultExt = ".vhd",
                              Filter = "Virtual Hard Disk (.vhd)|*.vhd"
                          };

            // Show open file dialog box
            var result = dlg.ShowDialog();

            // Process open file dialog box results
            if (result == true)
            {
                // Open document
                txtLiteralPath.Text = dlg.FileName;
                txtName.Text = dlg.SafeFileName;
            }
        }

        private void btnClearConn_Click(object sender, RoutedEventArgs e)
        {
            _csuploadtool.ClearConnection();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            _csuploadtool.AddMRW();
        }

        private void mnuOpen_Clicked(object sender, RoutedEventArgs e)
        {
            var dlg = new Microsoft.Win32.OpenFileDialog
            {
                DefaultExt = ".xml",
                Filter = "CsUploadTool OSUI Config file (.xml)|*.xml"
            };

            var result = dlg.ShowDialog();
            //if (result == true)
                //_csuploadtool = FileManagement.Open(dlg.FileName);
        }

        private void mnuSave_Clicked(object sender, RoutedEventArgs e)
        {
            //FileManagement.Save(_csuploadtool);
        }

        private void mnuSaveAs_Clicked(object sender, RoutedEventArgs e)
        {
            var dlg = new Microsoft.Win32.SaveFileDialog
            {
                DefaultExt = ".xml",
                Filter = "CsUploadTool OSUI Config file (.xml)|*.xml"
            };

            var result = dlg.ShowDialog();
            if (result == true)
            {
                //FileManagement.SaveAs(_csuploadtool,dlg.FileName);
            }

        }

        private void mnuExit_Clicked(object sender, System.Windows.Controls.ContextMenuEventArgs e)
        {
            
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            _csuploadtool.GetAffinity();
        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            _csuploadtool.GetLocation();
        }

        private void button6_Click(object sender, RoutedEventArgs e)
        {
            _csuploadtool.GetVmImage();
        }
    }
}
